<?php
// $Id: template.php,v 1.1.2.6.4.2 2011/01/11 01:08:49 dvessel Exp $

/**
 * The default group for NineSixty framework CSS files added to the page.
 */
define('CSS_NS_FRAMEWORK', -200);

/**
 * Implements hook_preprocess_html
 */
function vnexpress_preprocess_html(&$vars) {
  $vars['classes_array'][] = 'show-grid';
}

//Start /////////////////////////////////////////////////////
function vnexpress_link($variables) {	
  return '<a href="' . check_plain(url($variables['path'], $variables['options'])) . '"' . '>' . ($variables['options']['html'] ? $variables['text'] : check_plain($variables['text'])) . '</a>';
}

function vnexpress_menu_tree($variables) {
  return '<ul>' . $variables['tree'] . '</ul>';
}

function vnexpress_menu_link(array $variables) {
  $element = $variables['element'];
  $sub_menu = '';
  //$element['html'] = true;	
  if ($element['#below']) {
    $sub_menu = drupal_render($element['#below']);
  }
 // $output = l('<span class="pmenuL"></span><span class="pmenuM">'.$element['#title'].'</span><span class="pmenuR"></span>', $element['#href'], $element['#localized_options']);
 $output = l($element['#title'], $element['#href'], $element['#localized_options']);
  return '<li' . drupal_attributes($element['#attributes']) . '>' . $output . $sub_menu . "</li>\n";
}

function vnexpress_menu_tree_output($tree) {
  $build = array();
  $items = array();

  // Pull out just the menu links we are going to render so that we
  // get an accurate count for the first/last classes.
  foreach ($tree as $data) {
    if ($data['link']['access'] && !$data['link']['hidden']) {
      $items[] = $data;
    }
  }
  
  $router_item = menu_get_item();
  $num_items = count($items);
  $element['#menu_left_bg'] = '<li><div class="fl">'. theme('image',array('path'=>drupal_get_path('theme', 'vnexpress') .'/images/sep-pmenu.gif')).'</div><div class="pmenu-sep fl"></div></li>';
  foreach ($items as $i => $data) {
    $class = array();
	
    if ($i == 0) {
      $class[] = 'active';
	  $build[$data['link']['mlid']] = array('#html'=>true,'#href'=>'<front>','#type' => 'menu_link__' . $data['link']['menu_name'],'#title'=>'aaaa');
    }
    if ($i == $num_items - 1) {
      $class[] = 'last';
    }
    // Set a class for the <li>-tag. Since $data['below'] may contain local
    // tasks, only set 'expanded' class if the link also has children within
    // the current menu.
    if ($data['link']['has_children'] && $data['below']) {
      //$class[] = 'expanded';
    }
    elseif ($data['link']['has_children']) {
      $class[] = 'collapsed';
    }
    else {
      $class[] = '';
    }
    // Set a class if the link is in the active trail.
    if ($data['link']['in_active_trail']) {
      $class[] = 'active-trail';
      $data['link']['localized_options']['attributes']['class'][] = 'active-trail';
    }
    // Normally, l() compares the href of every link with $_GET['q'] and sets
    // the active class accordingly. But local tasks do not appear in menu
    // trees, so if the current path is a local task, and this link is its
    // tab root, then we have to set the class manually.
    if ($data['link']['href'] == $router_item['tab_root_href'] && $data['link']['href'] != $_GET['q']) {
      //$data['link']['localized_options']['attributes']['class'][] = 'active';
    }

    // Allow menu-specific theme overrides.
    $element['#theme'] = 'menu_link__' . $data['link']['menu_name'];
    $element['#attributes']['class'] = $class;
    $element['#title'] = $data['link']['title'];
	
    $element['#href'] = $data['link']['href'];
    $element['#localized_options'] = !empty($data['link']['localized_options']) ? $data['link']['localized_options'] : array();
    $element['#below'] = $data['below'] ? vnexpress_menu_tree_output($data['below']) : $data['below'];
    $element['#original_link'] = $data['link'];
    // Index using the link's unique mlid.
    $build[$data['link']['mlid']] = $element;
  }
  if ($build) {
    // Make sure drupal_render() does not re-order the links.
    $build['#sorted'] = TRUE;
    // Add the theme wrapper for outer markup.
    // Allow menu-specific theme overrides.
    $build['#theme_wrappers'][] = 'menu_tree__' . strtr($data['link']['menu_name'], '-', '_');
  }
	//die(print_r($build));
  return $build;
}

function vnexpress_links($variables) {
	//die(print_r($variables));	
  $links = $variables['links'];
  $attributes = $variables['attributes'];
  $heading = $variables['heading'];
  global $language_url;
  $output = '';

  if (count($links) > 0) {
    $output = '';

    // Treat the heading first if it is present to prepend it to the
    // list of links.
    if (!empty($heading)) {
      if (is_string($heading)) {
        // Prepare the array that will be used when the passed heading
        // is a string.
        $heading = array(
          'text' => $heading,
          // Set the default level of the heading.
          'level' => 'h2',
        );
      }
      $output .= '<' . $heading['level'];
      if (!empty($heading['class'])) {
        $output .= drupal_attributes(array('class' => $heading['class']));
      }
      $output .= '>' . check_plain($heading['text']) . '</' . $heading['level'] . '>';
    }

    $output .= '<ul' . drupal_attributes($attributes) . '>';
	
	$output .= '<li><div class="fl">'. theme('image',array('path'=>drupal_get_path('theme', 'vnexpress') .'/images/sep-pmenu.gif')).'</div><div class="pmenu-sep fl"></div>';
	$output .= "</li>";
	
    $num_links = count($links);
    $i = 1;

    foreach ($links as $key => $link) {
      $class = array($key);
	   $link['html'] = true;
      // Add first, last and active classes to the list of links to help out themers.
	  $submenu_home = '';
        if ($i == 1) {
			$class[] = 'active';
			$submenu_home = '<ul class="smenu-home">
				<li><a class="link-site ebank" href="http://ebank.vnexpress.net/" target="_blank">Ebank</a></li>
				<li><a class="link-site job" href="http://vieclam.vnexpress.net/" target="_blank">Việc làm</a>&nbsp;'.theme('image',array('path'=>drupal_get_path('theme', 'vnexpress') .'/images/sep.gif')).'&nbsp;</li>
				<li><a class="link-site game" href="http://gamethu.vnexpress.net/" target="_blank">Game thủ</a>&nbsp;'.theme('image',array('path'=>drupal_get_path('theme', 'vnexpress') .'/images/sep.gif')).'&nbsp;</li>
				<li><a class="link-site digital" href="http://sohoa.vnexpress.net/" target="_blank">Số hóa'.theme('image',array('path'=>drupal_get_path('theme', 'vnexpress') .'/images/sep.gif')).'&nbsp;</a></li>
				<li><a class="link-site star" href="http://ngoisao.net/" target="_blank">Ngôi sao</a>&nbsp;'.theme('image',array('path'=>drupal_get_path('theme', 'vnexpress') .'/images/sep.gif')).'&nbsp;</li>				
				<li style="padding-right: 5px;">'.theme('image',array('path'=>drupal_get_path('theme', 'vnexpress') .'/images/hotline_2.gif','attributes'=>array('class'=>'fl'))).'<p style="padding: 0pt; margin: 0pt; line-height: 17px; font-weight: bold;" class="fl link-site">0123.888.0123(HN) - 0129.233.3555(HCM)</p></li>
				<li>&nbsp;'.theme('image',array('path'=>drupal_get_path('theme', 'vnexpress') .'/images/sep.gif')).'&nbsp;</li>
				<li><a class="link-site" onclick="javascript:setAsHomePage(this)">'.theme('image',array('path'=>drupal_get_path('theme', 'vnexpress') .'/images/home.gif')).'</a></li>
				<li>&nbsp;'.theme('image',array('path'=>drupal_get_path('theme', 'vnexpress') .'/images/sep.gif')).'&nbsp;</li>
				<li><a class="link-site" href="/contactus/?id=webmaster@vnexpress.net">'.theme('image',array('path'=>drupal_get_path('theme', 'vnexpress') .'/images/mail.gif','attributes'=>array('alt'=>'Lien he toa soan'))).'</a></li></ul>';
        }		
        if ($i == $num_links) {
			$class[] = 'last';
			$output_line = '';
        }else{
		 $output_line = '<li class="mnser"></li>';
	    }
	  
      if (isset($link['href']) && ($link['href'] == $_GET['q'] || ($link['href'] == '<front>' && drupal_is_front_page()))
          && (empty($link['language']) || $link['language']->language == $language_url->language)) {
        //$class[] = 'active';
      }
      $output .= '<li' . drupal_attributes(array('class' => $class)) . '> ';
		//die(print_r($link));
      if (isset($link['href'])) {
        // Pass in $link as $options, they share the same keys.
        $output .= l('<span class="pmenuL"></span><span class="pmenuM">'.$link['title'].'</span><span class="pmenuR"></span>', $link['href'], $link);
      }
      elseif (!empty($link['title'])) {
        // Some links are actually not links, but we wrap these in <span> for adding title and class attributes.
        if (empty($link['html'])) {
          $link['title'] = check_plain($link['title']);
        }
        $span_attributes = '';
        if (isset($link['attributes'])) {
          $span_attributes = drupal_attributes($link['attributes']);
        }		
        $output .= '<span' . $span_attributes . '>' . $link['title'] . '</span>';
      }	
		
      $i++;
      $output .= $submenu_home.'</li>'.$output_line;
	  
    }
    $output .= '</ul>';
  }

  return $output;
}

//End////////////////////////////////////////////////////////////////////

/**
 * Preprocessor for page.tpl.php template file.
 */
function vnexpress_preprocess_page(&$vars, $hook) {

  // For easy printing of variables.
  $vars['logo_img'] = '';
  if (!empty($vars['logo'])) {
    $vars['logo_img'] = theme('image', array(
      'path'  => $vars['logo'],
      'alt'   => t('Home'),
      'title' => t('Home'),
    ));
  }
  $vars['linked_logo_img']  = '';
  if (!empty($vars['logo_img'])) {
    $vars['linked_logo_img'] = l($vars['logo_img'], '<front>', array(
      'attributes' => array(
        'rel'   => 'home',
        'title' => t('Home'),
      ),
      'html' => TRUE,
    ));
  }
  $vars['linked_site_name'] = '';
  if (!empty($vars['site_name'])) {
    $vars['linked_site_name'] = l($vars['site_name'], '<front>', array(
      'attributes' => array(
        'rel'   => 'home',
        'title' => t('Home'),
      ),
    ));
  }
  
  //die(print_r(drupal_render(menu_tree_output(menu_tree_all_data('main-menu')))));
  //die(print_r($vars['main_menu']));
 //die(print_r(menu_navigation_links('main-menu',1)));
  // Site navigation links.
  $vars['main_menu_links'] = '';
  if (isset($vars['main_menu'])) {
    $vars['main_menu_links'] = theme('links__system_main_menu', array(
      'links' => drupal_render(menu_tree_output(menu_tree_all_data('main-menu'))),//$vars['main_menu'],
      'attributes' => array(
        'id' => 'main-menu',
        
      ),
	  'html' => TRUE,
	  
      'heading' => array(
        'text' => t('Main menu'),
        'level' => 'h2',
        'class' => array('element-invisible'),
      ),
    ));
	$vars['main_menu_links'] = drupal_render(vnexpress_menu_tree_output(menu_tree_all_data('main-menu')));
	//die($vars['main_menu_links']);
  }
  $vars['secondary_menu_links'] = '';
  if (isset($vars['secondary_menu'])) {
    $vars['secondary_menu_links'] = theme('links__system_secondary_menu', array(
      'links' => $vars['secondary_menu'],
      'attributes' => array(
        'id'    => 'secondary-menu',
        'class' => array('inline', 'secondary-menu'),
      ),
      'heading' => array(
        'text' => t('Secondary menu'),
        'level' => 'h2',
        'class' => array('element-invisible'),
      ),
    ));
  }

}

/**
 * Contextually adds 960 Grid System classes.
 *
 * The first parameter passed is the *default class*. All other parameters must
 * be set in pairs like so: "$variable, 3". The variable can be anything available
 * within a template file and the integer is the width set for the adjacent box
 * containing that variable.
 *
 *  class="<?php print ns('grid-16', $var_a, 6); ?>"
 *
 * If $var_a contains data, the next parameter (integer) will be subtracted from
 * the default class. See the README.txt file.
 */
function ns() {
  $args = func_get_args();
  $default = array_shift($args);
  // Get the type of class, i.e., 'grid', 'pull', 'push', etc.
  // Also get the default unit for the type to be procesed and returned.
  list($type, $return_unit) = explode('-', $default);

  // Process the conditions.
  $flip_states = array('var' => 'int', 'int' => 'var');
  $state = 'var';
  foreach ($args as $arg) {
    if ($state == 'var') {
      $var_state = !empty($arg);
    }
    elseif ($var_state) {
      $return_unit = $return_unit - $arg;
    }
    $state = $flip_states[$state];
  }

  $output = '';
  // Anything below a value of 1 is not needed.
  if ($return_unit > 0) {
    $output = $type . '-' . $return_unit;
  }
  return $output;
}

/**
 * Implements hook_css_alter.
 * 
 * This rearranges how the style sheets are included so the framework styles
 * are included first.
 *
 * Sub-themes can override the framework styles when it contains css files with
 * the same name as a framework style. This mirrors the behavior of the 6--1
 * release of NineSixty warts and all. Future versions will make this obsolete.
 */
function vnexpress_css_alter(&$css) {
  global $theme_info, $base_theme_info;

  // Dig into the framework .info data.
  $framework = !empty($base_theme_info) ? $base_theme_info[0]->info : $theme_info->info;

  // Ensure framework CSS is always first.
  $on_top = CSS_NS_FRAMEWORK;

  // Pull framework styles from the themes .info file and place them above all stylesheets.
  if (isset($framework['stylesheets'])) {
    foreach ($framework['stylesheets'] as $media => $styles_from_960) {
      foreach ($styles_from_960 as $style_from_960) {
        // Force framework styles to come first.
        if (strpos($style_from_960, 'framework') !== FALSE) {
          $css[$style_from_960]['group'] = $on_top;
          // Handle styles that may be overridden from sub-themes.
          foreach (array_keys($css) as $style_from_var) {
            if ($style_from_960 != $style_from_var && basename($style_from_960) == basename($style_from_var)) {
              $css[$style_from_var]['group'] = $on_top;
            }
          }
        }
      }
    }
  }
}
